package com.demo.afternoon;

/**
 * @ClassName 死锁
 * @Description TODO
 * @Author tianqinglong
 * @Date 20:11 2022/7/14
 * @Version 1.0
 **/
public class 死锁 {
    public static void main(String[] args) {
        Makeup g1=new Makeup(0, "灰姑凉");
        Makeup g2=new Makeup(1, "白雪公主");
        g1.start();
        g2.start();
    }
}


//口红
class Lipstick {

}

//镜子
class Mirror {

}
class Makeup extends Thread {
  static  Lipstick lip=  new Lipstick();
   static Mirror mirror= new Mirror();
   int choice;
   String grilName;
    public Makeup(int choice, String grilName) {
        this.choice = choice;
        this.grilName = grilName;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lip) {
                System.out.println(this.grilName + "获得口红的锁");
                Thread.sleep(1000);
//				synchronized (mirror) {
//					System.out.println(this.grilName+"获得镜子的锁");
//					Thread.sleep(1000);   			//放在这里会造成死锁
//				}
            }
            synchronized (mirror) {
                System.out.println(this.grilName + "获得镜子的锁");
                Thread.sleep(1000);
            }

        } else {
            synchronized (mirror) {
                System.out.println(this.grilName + "获得镜子的锁");
                Thread.sleep(2000);
//				synchronized (lip) {
//					System.out.println(this.grilName+"获得口红的锁");
//					Thread.sleep(1000);				 //放在这里会造成死锁
//				}
            }
            synchronized (lip) {
                System.out.println(this.grilName + "获得口红的锁");
                Thread.sleep(1000);
            }

        }
    }
}